<?php namespace App\Http\Controllers\Admin;

use App\Category;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Str;
use Request;
use Redirect;
use Response;

class CategoryController extends Controller {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		$categories = Category::with('products')->paginate();
		return view('admin.categories.index', compact('categories'));
	}

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		return view('admin.categories.create');
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		$category = new Category;
		$title = Request::input('title');
		$image = Request::file('image');
		$big_image = Request::file('big_image');
		$description = Request::input('description');
		if (Request::hasFile('image') && Request::hasFile('big_image'))
		{
			$file_name = 'category-'.time().'.'.$image->getClientOriginalExtension();
			$big_file_name = 'category-header'.time().'.'.$image->getClientOriginalExtension();
			$uploadSuccess = $image->move(public_path().'/images', $file_name);
			$uploadSuccess2 = $big_image->move(public_path().'/images', $big_file_name);
			$file_path = 'images/'.$file_name;
			$big_file_path = 'images/'.$big_file_name;
			if($uploadSuccess && $uploadSuccess2) {
				$category->title = $title;
				$category->slug = Str::slug($title);
				$category->image_url = $file_path;
				$category->big_image_url = $big_file_path;
				$category->description = $description;
				$category->save();

				return Redirect::route('admin.category.index')->with('message', '上传成功');
			}
		}

		return Response::json(['message' => 'upload error', 'status' => 'error', 'statusCode' => 500]);
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		$category = Category::find($id);
		return view('admin.categories.edit', compact('category'));
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		$category = Category::find($id);
		$title = Request::input('title');
		$image = Request::file('image');
		$big_image = Request::file('big_image');
		$description = Request::input('description');
		if (Request::hasFile('image') && Request::hasFile('big_image'))
		{
			$file_name = 'category-'.time().'.'.$image->getClientOriginalExtension();
			$big_file_name = 'category-header'.time().'.'.$image->getClientOriginalExtension();
			$uploadSuccess = $image->move(public_path().'/images', $file_name);
			$uploadSuccess2 = $big_image->move(public_path().'/images', $big_file_name);
			$file_path = 'images/'.$file_name;
			$big_file_path = 'images/'.$big_file_name;
			if($uploadSuccess && $uploadSuccess2) {
				$category->title = $title;
				$category->slug = Str::slug($title);
				$category->image_url = $file_path;
				$category->big_image_url = $big_file_path;
				$category->description = $description;
				$category->update();

				return Redirect::route('admin.category.index')->with('message', '上传成功');
			}
		}

		return Response::json(['message' => 'upload error', 'status' => 'error', 'statusCode' => 500]);
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}

}
